﻿using IpPortDemo.Entity;
using IpPortDemo.Tools;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

namespace IpPortDemo.Controllers
{
    /// <summary>
    /// 用户控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        /// <summary>
        ///  导出 
        /// </summary>
        /// <returns></returns>
        [HttpGet("export")]
        public IActionResult ExportExcel()
        {
            // 模拟数据
            var data = new List<User>
            {
                new User { Id = 1, UserName = "codeYear", RealName="编程光年", Age = 35,Gender="男" },
                new User { Id = 2, UserName = "kay", RealName="kay2025", Age = 35 ,Gender="男" }
            };

            //说明导出的表头中文名称原理是根据实体的[Description("用户名")] 属性 进行匹配的
            string file = new ExcelHelper<User>().ExportToExcel("用户列表.xls",
                                                                          "用户列表",
                                                                          data,
                                                                         new string[] { "Id", "UserName", "RealName", "Gender", "Age" }, "");

            return Ok(new { file = file });
        }

        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        [HttpPost("import")]
        public IActionResult ImportExcel(string filePath)
        {
            //说明导入的原理是根据实体的[Description("用户名")] 属性 进行匹配的
            List<User> list = new ExcelHelper<User>().ImportFromExcel(filePath);
           
            var obj = new { list };

            return Ok(new { obj });
        }
    }
}
